/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cw.view;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import org.cw.bean.Establishment;
import org.cw.bo.EstablishmentBO;

/**
 *
 * @author SVN - Team
 */
public class FromAddEstablistment extends Form implements CommandListener, Runnable {

    private EstablishmentBO estBO = EstablishmentBO.getInstance();
    private static final String TITLE = "New Establistment";
    private TextField txtID, txtName, txtTypeOfFood, txtLocation;
    private ChoiceGroup cbbTypeOfEst;
    private Command cmdAdd, cmdCancel, cmdOK;
    private Alert success, err;
    private FromListEstablishments frmLis;
    private MReviewer curDisplay;
    private static final String typeOfEst[] = {"Café", "Bar", "Restaurant"};
    private Image iconAdd;
    private StringItem titleAdd;
    private StringItem txtNote;

    public FromAddEstablistment(MReviewer curDisplay) {
        super(TITLE);
        this.curDisplay = curDisplay;
        init();

    }

    private void init() {
        try {
            iconAdd = Image.createImage("/add.png");
        } catch (Exception e) {
        }
        append(iconAdd);
        titleAdd = new StringItem("", "Add New Establishment");
        titleAdd.setLayout(StringItem.LAYOUT_2);
        append(titleAdd);
        try {
            Image[] imgs = {Image.createImage("/cafe_coffee.png"), Image.createImage("/drink_bar_cocktails.png"), Image.createImage("/restaurant.png")};
            txtID = new TextField("ID (*)", "", 31, TextField.ANY);
            txtName = new TextField("Name of Establistment (*)", "", 50, TextField.ANY);
            cbbTypeOfEst = new ChoiceGroup("Type of establishment (*)", Choice.POPUP, typeOfEst, imgs);
            txtTypeOfFood = new TextField("Type Of Food", "", 50, TextField.ANY);
            txtLocation = new TextField("Location", "", 50, TextField.ANY);
            txtNote = new StringItem("", "Note: The fields with (*) are required to complete the information.");

            cmdAdd = new Command("add", Command.OK, 0);
            cmdCancel = new Command("Cancel", Command.CANCEL, 1);

            append(txtID);
            append(txtName);
            append(cbbTypeOfEst);
            append(txtTypeOfFood);
            append(txtLocation);
            append(txtNote);

            addCommand(cmdAdd);
            addCommand(cmdCancel);
            setCommandListener(this);
        } catch (IOException ex) {
        }
    }

    private void initData() {
        
    }
    private Image iconSuccess;
    public void commandAction(Command c, Displayable d) {
        Thread t;
        if (c == cmdAdd) {
            boolean chk = checkValid();
            if (chk) {
                Establishment est = new Establishment();
                est.setId(txtID.getString());
                est.setName(txtName.getString());
                est.setType(cbbTypeOfEst.getString(cbbTypeOfEst.getSelectedIndex()));
                est.setTypeOfServices(txtTypeOfFood.getString());
                est.setLocation(txtLocation.getString());
                boolean isChk = estBO.add(est);
                if (isChk) {
                    MReviewer.dataStore.addElement(est);
                    System.out.println("add new success full");
                    try {
                        iconSuccess = Image.createImage("/dialog_apply.png");
                    }catch(Exception ex){}
                    success = new Alert("Message", "Add new Establishment successfully!", null, AlertType.ALARM);
                    success.setTimeout(2000);
                    cmdOK = new Command("OK", Command.OK, 0);
                    success.addCommand(cmdOK);
                    success.setCommandListener(this);
                    Display.getDisplay(curDisplay).setCurrent(success, this);
                    flag = false;
                    t = new Thread(this);
                    t.start();
                } else {
                    err = new Alert("Error!", "Error, Plase try again!", null, AlertType.ERROR);
                    Display.getDisplay(curDisplay).setCurrent(err, this);
                }
            }
        } else if (c == cmdCancel) {
            frmLis = new FromListEstablishments(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(frmLis);
        } else if (c == cmdOK) {
            flag = true;
            frmLis = new FromListEstablishments(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(frmLis);
        }
    }

    private boolean checkValid() {
        boolean isChk = true;
        isChk = estBO.checkHasId(txtID.getString());
        if (isChk) {
            err = new Alert("Error!", "Id of establishment is exist!", null, AlertType.ERROR);
            Display.getDisplay(curDisplay).setCurrent(err, this);
            return false;
        } else if (txtID.getString().equals("")) {
            err = new Alert("Error!", "Id of establishment is not empty!", null, AlertType.ERROR);
            Display.getDisplay(curDisplay).setCurrent(err, this);
            return false;
        } else if (txtName.getString().equals("")) {
            err = new Alert("Error!", "Name of establishment is not empty!", null, AlertType.ERROR);
            Display.getDisplay(curDisplay).setCurrent(err, this);
            return false;
        }
        return true;
    }
    private boolean flag = false;

    public void run() {
        while (true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
            }
            if (!flag) {
                flag = true;
                frmLis = new FromListEstablishments(curDisplay);
                Display.getDisplay(curDisplay).setCurrent(success, frmLis);
            } else {
                break;
            }
        }
    }
}
